# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        潘炎珍  
# Name:         GetInfo
# Description:
# Author:       98745
# Date:         2019/4/6
#-------------------------------------------------------------------------------

import requests
from bs4 import *
import re
from projects.douban_spider.Dbmanage import *

def detail(info_text,s,e):
    '''获取不在标签内的信息'''
    try:
        start=info_text.find(s)+len(s)
        end=info_text.find(e,start)
        if end-start>0:
            string=info_text[start:end].strip()
        return string
    except Exception as e:
        print(e)


def getinfo(urls_list):
    '''获取每本书的详细信息'''
    for url in urls_list:
        try:
            # url='https://book.douban.com/subject/25862578/'
            # url='https://book.douban.com/subject/2567698/'
            headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
            response=requests.get(url,headers=headers)
            soup=BeautifulSoup(response.text,'lxml')
            data={}
            title=soup.find('span',{'property':"v:itemreviewed"}).text.strip()
            data['title']=title
            info=soup.find('div',{'id':"info"})
            # print(info.text)
            info_text0 = re.subn(' ', '', info.text)[0]
            info_text=re.subn('\n+','-',info_text0)[0]
            # print('-------')
            # print(info_text)
            authod=detail(info_text,'作者: -','-出版社').strip()
            data['authod']=authod
            price=detail(info_text,'定价:','-')
            data['price']=price
            des = soup.find('div', {'class': "rating_self clearfix"})
            # print(des)
            grade = des.find('strong', {'class': "ll rating_num"}).text.strip()
            # print('------')
            # des_grade=des.find('strong', {'class':"ll rating_num"})
            # print(des_grade)
            # # grade=des_grade.string
            data['grade'] = grade
            votenum = des.find('span', {'property': "v:votes"}).text
            data['votenum'] = votenum
            if len(data)==5:
                insert(db, data)
        except Exception as e:
            print(e)
        # return data
        # insert(db,data)


# if __name__=='__main__':
#     data=getinfo()
#     print(data)
#     # print(data['price'])
#     # print(type(data['price']))











